#import "ToDoGroupEditView.h"

@implementation ToDoGroupEditView

- (id)initWithFrame:(struct CGRect)frame
{
	self = [super initWithFrame:frame];
	
	[[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(reloadTableData)
												 name:@"ToDoGroupsChangeNotification"
											   object:nil];

	
	toDoManager = [ToDoManager sharedToDoManager];
	td = [MobileToDoListApplication sharedInstance];
	
	editMode = YES;
	imageHeight = 23;
	
	checkboxOn 	= [UIImage applicationImageNamed:@"checkbox_on.png"];
	checkboxOff = [UIImage applicationImageNamed:@"checkbox_off.png"];
	
	//Get full screen app rect
	struct CGRect rect = [UIHardware fullScreenApplicationContentRect];
	rect.origin.x = rect.origin.y = 0.0f;
	
	navBar = [[UINavigationBar alloc] init];
	[navBar setFrame:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, 45.0f)];
	[navBar showButtonsWithLeftTitle:@"Back" rightTitle:@" + " leftBack:YES];
	[navBar setDelegate:self];
	[navBar enableAnimation];
	
	[self buildTable];
	
	[self addSubview:navBar];
	
		
	return self;
}

// Build the Table
- (void)buildTable
{
	struct CGRect rect = [UIHardware fullScreenApplicationContentRect];
	rect.origin.x = rect.origin.y = 0.0f;
	
	sectionList = [[UISectionList alloc] initWithFrame:CGRectMake(rect.origin.x, 45.0f, rect.size.width, rect.size.height - 45.0f) showSectionIndex:NO];
	[sectionList setDataSource:self];
	[sectionList reloadData];
	table = [sectionList table];
	
	// please show the sectionList even then when there are not more than 20 todos
	[table setShouldHideHeaderInShortLists:NO];
	//Create table to hold all todos
	//table = [[UITable alloc] initWithFrame:CGRectMake(rect.origin.x, 45.0f, rect.size.width, rect.size.height - 45.0f)];
	UITableColumn *col = [[UITableColumn alloc] initWithTitle:@"Todos" identifier:@"todos" width:rect.size.width];
	[table addTableColumn:col];
	[table setSeparatorStyle:1];
	[table setRowHeight:40];
	[table setDataSource:self];
	[table setDelegate:self];
	[table enableRowDeletion:YES];
	[table reloadData];
	
	[self addSubview:sectionList];
}


- (void)reloadTableData
{
	[table reloadData];
}


- (void)createNewToDo
{
	ToDo *myToDo = [[ToDo alloc] init];
	[toDoManager addToDo:myToDo];
	[toDoManager orderTodosByGroup];
	[self reloadTableData];
}

#pragma mark ----------Delegate Methods-----------

- (void)navigationBar:(UINavigationBar*)bar buttonClicked:(int)button
{
	if(button == 1){
		[td showToDoPreferencesWithTransition:2];
	}
	if(button == 0){
		[toDoManager addGroup:@""];
		[toDoManager setCurrentGroup:[toDoManager getNextUnnamedGroup]];
		[td showToDoGroupDetailWithTransition:1];
	}
}

- (void)tableRowSelected:(NSNotification *)notification
{
	if(([[toDoManager getPossibleGroups] count] == 0) || ([[toDoManager getPossibleGroups] count] <= [table selectedRow]) ) {
		return false;
	}
	
	if(editMode) {
		[toDoManager setCurrentGroup:[table selectedRow]];
		[td showToDoGroupDetailWithTransition:1];
	}
}

#pragma mark ----------Datasource Methods-----------

- (int)numberOfRowsInTable:(UITable *)table
{
	return [[toDoManager getPossibleGroups] count];
}

// Deletion (not implementet yet)
- (BOOL) table:(UITable *) table canDeleteRow:(int) row {
	return YES;
}


- (UITableCell *)table:(UITable *)table cellForRow:(int)row column:(int)col
{	
	CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
	float transparentComponents[4] = {0, 0, 0, 0};
	float grayComponents[4] = {0.55, 0.55, 0.55, 1};
	float blueComponents[4] = {0.208, 0.482, 0.859, 1};
	
	//Create description label
	UITextLabel *descriptionLabel = [[UITextLabel alloc] initWithFrame:CGRectMake(10.0f, 3.0f, 210.0f, 30.0f)];
	[descriptionLabel setText:[[toDoManager getPossibleGroups] objectAtIndex:row]];
	[descriptionLabel setWrapsText:YES];
	[descriptionLabel setBackgroundColor:CGColorCreate(colorSpace, transparentComponents)];
	
	UITableCell *cell = [[UITableCell alloc] init];
	
	[cell addSubview:descriptionLabel];
	
	[cell setShowDisclosure:YES];
	return cell;
}


#pragma mark ----------SectionList Methods-----------

- (int)numberOfSectionsInSectionList:(UISectionList *)aSectionList {
	return 1;
}
        
- (NSString *)sectionList:(UISectionList *)aSectionList titleForSection:(int)section {    
	return @"Groups";
}       
        
- (int)sectionList:(UISectionList *)aSectionList rowForSection:(int)section {
	return 0;
}

@end